package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Claim;
import cn.wolfcode.crm.domain.ClaimItem;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.ClaimItemMapper;
import cn.wolfcode.crm.query.ClaimQueryObject;
import cn.wolfcode.crm.service.IClaimService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.LogicException;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("claim")
public class ClaimController {

    @Autowired
    IClaimService claimService;
    @Autowired
    ClaimItemMapper claimItemMapper;

    @RequestMapping("view")
    @RequiresPermissions(value = {"claim:view","理赔页面"},logical = Logical.OR)
    public String view() {
        return "claim";
    }

    @RequestMapping("view4item")
    @RequiresPermissions(value = {"claim:view","理赔明细页面"},logical = Logical.OR)
    public String view4item() {
        return "claimItem";
    }

    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"claim:query","理赔列表"},logical = Logical.OR)
    public PageResult query(ClaimQueryObject qo) {
        return claimService.query(qo);
    }

    @RequestMapping("itemQuery")
    @ResponseBody
    @RequiresPermissions(value = {"claim:itemQuery","理赔列表"},logical = Logical.OR)
    public PageResult itemQuery(ClaimQueryObject qo) {
        return claimService.itemQuery(qo);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Claim> selectAll() {
        return claimService.selectAll();
    }

    @RequestMapping("changeState")
    @ResponseBody
    public JsonResult changeState(Long id) {
        try {
            //录入审核人和时间
            Employee currentUser = UserContext.getCurrentUser();
            Claim claim = claimService.selectByPrimaryKey(id);
            claim.setAuditTime(new Date());
            claim.setAuditUser(currentUser);
            //改变审核状态
            claimService.updateByAudit(claim);
            claimService.changeState(id);
            return JsonResult.success("");
        }
        catch (LogicException e) {
            e.printStackTrace();
            return JsonResult.mark(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("操作失败");
        }
    }

    @RequestMapping("changeItemState")
    @ResponseBody
    public JsonResult changeItemState(Long id) {
        try {
            claimService.changeItemState(id);
            return JsonResult.success("");
        }
        catch (LogicException e) {
            e.printStackTrace();
            return JsonResult.mark(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("操作失败");
        }
    }

    @RequestMapping("validateStaging")
    @ResponseBody
    public boolean validateUsername(String staging, Long id) {
        String staing = claimService.selectStagingByStaging(staging);
        return staing != null;
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Claim claim) {
        try {
            if (claim.getId() != null) {
                claimService.updateByPrimaryKey(claim);
            } else {
                claimService.insert(claim);
            }
            return JsonResult.success("");
        }catch (LogicException e) {
            e.printStackTrace();
            return JsonResult.mark(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("保存失败");
        }
    }

    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            claimService.deleteByPrimaryKey(id);
            return JsonResult.success("");
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("操作失败");
        }
    }

    @RequestMapping("getCliamItemByClaimId")
    @ResponseBody
    public ClaimItem getCliamItemByClaimId(Long claimId) {
        return claimService.getCliamItemByClaimId(claimId);
    }

    @RequestMapping("selectAllEmp")
    @ResponseBody
    public List<Employee> selectAllEmp() {
        return claimService.selectAllEmp();
    }

    @RequestMapping("update4Dispose")
    @ResponseBody
    public JsonResult update4Dispose(ClaimItem claimItem) {
        try {
            ClaimItem item = claimItemMapper.selectByPrimaryKey(claimItem.getId());
            if (item.getState() != null && item.getState() != false){
                throw new LogicException("数据已确认处理成功,无法再次处理");
            }
            claimService.update4Dispose(claimItem);
            return JsonResult.success("");
        }catch (LogicException e) {
            e.printStackTrace();
            return JsonResult.mark(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("保存失败");
        }
    }
}
